package com.gmail.deathswing.audiorecoder;

import java.io.IOException;

import android.app.Activity;
import android.content.res.Resources.Theme;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity implements View.OnClickListener {

	private boolean isRecoding = false;
	private boolean isPlaying = false ;
	private Button mBtnRecord;
	private Button mBtnPlay;
	private MediaPlayer mPlayer ;
	private MediaRecorder mRecorder;
	private TextView mTVTimer ;
	
	private Handler mHandler = new Handler(new Handler.Callback() {
		
		@Override
		public boolean handleMessage(Message msg) {
			String info = msg.arg1 /1000 + "/" + msg.arg2 /1000 ;
			mTVTimer.setText(info);
			return true;
		}
	});
	

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		mBtnRecord = (Button) findViewById(R.id.btn_record);
		mBtnRecord.setOnClickListener(this);

		mBtnPlay = (Button) findViewById(R.id.btn_play);
		mBtnPlay.setOnClickListener(this);
		
		mTVTimer = (TextView) findViewById(R.id.tv_timer);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_play:
			play();
			break;
		case R.id.btn_record:
			recode();
			break;
		}
	}
	
	private void play(){
		isPlaying = !isPlaying ;
		if(isPlaying){
			try {
				mBtnPlay.setText("停止播放");
				mPlayer =new MediaPlayer();
				mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
				mPlayer.setDataSource("/sdcard/test.amr");
				mPlayer.prepare();
				
				String info = mPlayer.getCurrentPosition() / 1000 + "/" +mPlayer.getDuration() / 1000;
				mTVTimer.setText(info);
				
				new Thread(new Runnable() {
					
					@Override
					public void run() {
						while(mPlayer.isPlaying()){
							Message msg = new Message();
							msg.arg1 = mPlayer.getCurrentPosition();
							msg.arg2 = mPlayer.getDuration() ;
							mHandler.sendMessage(msg);
						}
					}
				}).start();
				
				mPlayer.start();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}else{
			mPlayer.stop();
			mPlayer.release();
		}
	}

	private void recode() {
		isRecoding = !isRecoding;

		if (isRecoding) {
			mBtnRecord.setText("停止录音");
			try {
				mRecorder = new MediaRecorder();
				mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
				mRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
				mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
				mRecorder.setOutputFile("/sdcard/test.amr");
				mRecorder.prepare();
				mRecorder.start();
			} catch (IOException e) {
				e.printStackTrace();
			}

		} else {
			mBtnRecord.setText("开始录音");
			if (mRecorder != null) {
				mRecorder.stop();
				mRecorder.release();
				mRecorder = null;
			}

		}
	}
}
